﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxPro;
using Xiaov.SmartWeb.Model.ActiveRecord;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;
using Xiaov.SmartWeb.Model;

namespace Xiaov.SmartWeb.Application.SysAdmin
{
  public partial class Admin_Module : BasePage
  {

    protected void Page_Load(object sender, EventArgs e)
    {
      //权限横切
      PM_Filter();
      AjaxPro.Utility.RegisterTypeForAjax(typeof(Admin_Module));
    }

    [AjaxMethod]
    public bool Save(object moduleid, object modulename, object parentid, object moduleurl, object mtarget, object actionstring, object show, object disable, object sortby, object comment)
    {
      bool rtn = false;
      try
      {
        Xiaov.SmartWeb.Model.ActiveRecord.S_PM_MODULE model = new S_PM_MODULE();
        model.MODULEID = moduleid.ToString();
        if (string.IsNullOrEmpty(model.MODULEID))
        {
          model.MODULEID = Guid.NewGuid().ToString();
        }
        else
          model = S_PM_MODULE.Find(moduleid);

        model.MODULENAME = modulename.ToString();
        model.PARENTMODULEID = parentid.ToString();
        model.MODULEURL = moduleurl.ToString();
        model.MTARGET = mtarget.ToString();
        model.ACTIONSTRING = actionstring.ToString();
        if (show == null)
          show = "false";
        model.SHOW = (show.ToString() == "false" ? "0" : "1");

        if (disable == null)
          disable = "false";
        model.DISABLED = (disable.ToString() == "false" ? "0" : "1");

        model.SORTBY = sortby.ToInt32();
        model.MCOMMENT = comment.ToString();
        model.LOCKED = "0";
        model.Save();
        //model.RefreshCache();
        //重置缓存
        Xiaov.SmartWeb.Infrastructure.Cache.CacheFactroy.Instance().RemoveCache();
        rtn = true;
      }
      catch
      {
        rtn = false;
      }
      return rtn;
    }

    [AjaxMethod]
    public bool DeleteNode(object nodeid)
    {
      bool rtn = false;
      try
      {
        S_PM_MODULE model = S_PM_MODULE.Find(nodeid);
        S_PM_ROLE_PERMISSION.DeleteAll("MODULEID='" + nodeid + "'");
        model.Delete();
        //model.RefreshCache();
        //重置缓存
        Xiaov.SmartWeb.Infrastructure.Cache.CacheFactroy.Instance().RemoveCache();
        rtn = true;
      }
      catch
      {
        rtn = false;
      }
      return rtn;
    }

    [AjaxMethod]
    public bool ClearNode(object nodeid)
    {
      bool rtn = false;
      try
      {
        S_PM_MODULE model = new S_PM_MODULE();
        IList<S_PM_MODULE> list = S_PM_MODULE.FindAllByProperty("PARENTMODULEID", nodeid.ToString());
        foreach (var q in list)
        {
          S_PM_ROLE_PERMISSION.DeleteAll("MODULEID='" + q.MODULEID + "'");
        }
        S_PM_MODULE.DeleteAll("MODULEID='" + nodeid + "' or PARENTMODULEID='" + nodeid + "'");
        //model.RefreshCache();
        //重置缓存
        Xiaov.SmartWeb.Infrastructure.Cache.CacheFactroy.Instance().RemoveCache();
        rtn = true;
      }
      catch
      {
        rtn = false;
      }
      return rtn;
    }
  }
}